package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"litter-api/app/model"
	"litter-api/app/util"
	"net/http"
)

func CheckUserLogin() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		token := ctx.Request.Header.Get("Token")
		if token == "" {
			ctx.AbortWithStatusJSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "授权失败!",
				"data": "",
			})
		}
		token = token[8:]
		fmt.Println(token)
		jwt := util.Jwt{}
		claims, err := jwt.ParsingToken(token)
		if err != nil {
			ctx.AbortWithStatusJSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "授权失败!",
				"data": "",
			})
		}

		model.SetUser(claims.OpenId)
		//fmt.Println(claims.OpenId)
		ctx.Next()
	}
}
